<script>


    // 加载修改商品Modal
    function loadItemEditModal(itemID) {
        //清空表单验证状态
        $("input").removeClass("is-invalid");
        $("#itemEditModal").attr("data-item-id", itemID)
        console.log("debug")
        //loadFileinput()
        $.ajax({
            type: 'GET',
            url: '/get/Item/selectedByItemID',
            data: { itemID: parseInt(itemID) },
            success: (resJson) => {
                if (!resJson.success) {
                    alert(resJson.errorInfo)
                } else {
                    item = resJson.data.body
                    // 将商品信息赋值给全局变量
                    item_g = item
                    $("#itemName").val(item.ItemName)
                    $("#itemPrice").val(item.ItemPrice)
                    $("#itemDescription").val(item.ItemDescription)
                    $("#itemOldNewRate").val(item.ItemOldNewRate)
                    $("#coverFileName").val(item.CoverFileName)
                    $("#remarks").val(item.Remarks)
                    if (item.DeliverByPost) {
                        $("#deliverByPost").prop('checked', true)
                    }
                    if (item.DeliverByFace) {
                        $("#deliverByFace").prop('checked', true)
                    }
                    if (item.DeliverNoNeed) {
                        $("#deliverNoNeed").prop('checked', true)
                    }
                }
            }
        })
    }

    // 加载用户信息
    function loadUserInfo(token) {
        $.ajax({
            type: "GET",
            url: "/get/User/fullInfo",
            headers: { 'Authorization': `Bearer ${token}` },
            success: (resJson) => {
                if (!resJson.success) {
                    // ! 如果不成功, 跳转至登录页
                    alert('登录已经过期, 请前往重新登录~')
                    location.href = '/login'
                } else {
                    console.log(resJson)
                    userInfo = resJson.data.body
                    // √ 如果成功, 补充页面信息
                    $("#UserNameArea").html(`${userInfo.UserName}`)
                    $("#UserDescriptionArea").html(`${userInfo.UserDescription}`)
                    $("#UserIDArea").html(`站内ID: ${userInfo.UserID}`)
                    $("#UserWeChatArea").html(`微信: ${userInfo.UserWeChat}`)
                    $("#UserQQArea").html(`QQ: ${userInfo.UserQQ}`)
                    if(userInfo.PortraitFileName!='DEFAULT.PNG'){
                        $("#PortraitArea").attr('src', `https://xib-user-portrait.oss-cn-beijing.aliyuncs.com/${userInfo.PortraitFileName}?x-oss-process=style/portrait_home_page`)
                    }
                    // √ 如果成功, 补充全局信息
                    user_info.userName = userInfo.UserName
                    user_info.userQQ = userInfo.UserQQ
                    user_info.userDescription = userInfo.UserDescription
                    user_info.userWeChat = userInfo.UserWeChat
                    user_info.portraitFileName = userInfo.PortraitFileName

                }
            }
        })
    }


    // 加载用户页商品标签
    function loadHomeItemLabel(token, callback) {
        $.ajax({
            type: "GET",
            url: "/get/Item/homeItemLabel",
            headers: { 'Authorization': `Bearer ${token}` },
            success: (resJson) => {
                if (resJson.success == false) {
                    // 获取失败
                    if (resJson.errorInfo == 'SQL_NO_RESULT') {
                        console.log('未发布商品')
                    } else {
                        alert('获取用户页商品标签失败: ' + resJson.errorInfo)
                    }
                } else {
                    // 获取成功
                    $("#homeItemLabelArea").html(resJson.data.body)
                    callback()
                }
            }
        })

    }

    // 加载 fileinput插件
    function loadFileinput() {
        $("#uploadfileArea").css("display","")
        $("#showFileInputItem").css("display", "none")
        $("#uploadfile").fileinput({
            allowedFileTypes: ['image'],
            language: 'zh',
            theme: 'fas',
            uploadUrl: '/submit/file/itemCover',
            async: false,
            maxFileCount: 1,
            allowedFileExtensions: [],

            ajaxSettings: {
                dataType: 'json'
            },
            maxFileSize: 10 * 1024,
        })
        $("#uploadfile").on('fileuploaded', function (event, data, previewId, index) {
            if (data.response.success) {
                $("#coverFileName").val(data.response.data.filename_full);
                item_g.CoverFileHWRatio = data.response.data.file_hw_ratio
            } else {
                alert("上传失败, 错误信息：", data.response.errorInfo);
            }
        })
    }

    // 加载 fileinput插件(上传头像)
    function loadFileinputPortrait() {
        $("#uploadPortrait").css("display","")
        $("#showFileInputPortrait").css("display", "none")
        $("#uploadPortrait").fileinput({
            allowedFileTypes: ['image'],
            language: 'zh',
            theme: 'fas',
            uploadUrl: '/submit/file/userPortrait',
            async: false,
            maxFileCount: 1,
            allowedFileExtensions: [],

            ajaxSettings: {
                dataType: 'json'
            },
            maxFileSize: 5 * 1024,
        })
        $("#uploadPortrait").on('fileuploaded', function (event, data, previewId, index) {
            if (data.response.success) {
                $("#portraitFileName").val(data.response.data.filename_full);
            } else {
                alert("上传失败, 错误信息：", data.response.errorInfo);
            }
        })
    }

    // 加载警告/确认modal
    function loadDeleteAlertModal(itemIDStr) {
        itemID = parseInt(itemIDStr)
        $("#deleteAlertModal").attr('data-item-id', itemID)
    }

    function loadOffShelfAlertModal(itemIDStr) {
        itemID = parseInt(itemIDStr)
        $("#offShelfAlertModal").attr('data-item-id', itemID)
    }
    

    function loadUserEditModal(){
        $("#portraitFileName").val(user_info.portraitFileName)
        $("#userDescription").val(user_info.userDescription)
        $("#userQQ").val(user_info.userQQ)
        $("#userWeChat").val(user_info.userWeChat)
    }
</script>